﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

//Added
using System.IO;
using GELib;

namespace GameEditor
{
    /// <summary>
    /// Interaction logic for dlg_NewProject.xaml
    /// </summary>
    public partial class dlg_NewProject : Window
    {
        #region "Variables, Constructor, Properties, Loading"

        public dlg_NewProject()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LangManager.langLoaded += new EventHandler(changeLangTexts);
            changeLangTexts(null, EventArgs.Empty);

            txtAuthors.Text = Environment.UserName;
            txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.Personal);// +"\\" + "Project1";
        }

        void changeLangTexts(object sender, EventArgs e)
        {
            this.Title = LangManager.getPhrase("nPj_Title");
            txbTitle.Text = LangManager.getPhrase("nPj_ProjectTitle");
            txbAuthors.Text = LangManager.getPhrase("nPj_Authors");
            txbInfos.Text = LangManager.getPhrase("nPj_Infos");
            txbLocation.Text = LangManager.getPhrase("nPj_Path");
        }
        #endregion


        #region "Methods"
        private void cancel(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void OK(object sender, RoutedEventArgs e)
        {
            // Create project directory
            String dirPath = txtPath.Text.Trim() + "\\" + txtTitle.Text.Trim();
            try
            {
                if (!Directory.Exists(dirPath))
                    Directory.CreateDirectory(dirPath);

                DirectoryInfo DI = new DirectoryInfo(dirPath);

                GEProject.createProject(dirPath, txtTitle.Text.Trim(), txtAuthors.Text.Trim());

                // TODO : open new project
                GEProject.closeProject();
                GEProject.openProject(dirPath + "\\" + txtTitle.Text + ".GEPj");

                this.Close();
            }
            catch { MessageBox.Show("Impossible to create '" + dirPath + "' \n Please choose an other directory."); }
            
        }

        private void browse(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog FBD = new System.Windows.Forms.FolderBrowserDialog();
            FBD.Description = txtPath.Text;
            FBD.SelectedPath = txtPath.Text;
            FBD.ShowDialog();

            txtPath.Text = FBD.SelectedPath;
        }
        #region "Form check"
        private void txtTitle_TextChanged(object sender, TextChangedEventArgs e)
        {
            checkCanPressOK();
        }
        private void txtPath_TextChanged(object sender, TextChangedEventArgs e)
        {
            checkCanPressOK();
        }
        private void txtAuthors_TextChanged(object sender, TextChangedEventArgs e)
        {
            checkCanPressOK();
        }
        private void checkCanPressOK()
        {
            if (this.IsLoaded)
                btnOK.IsEnabled = ((txtTitle.Text.Trim() != "") & (txtPath.Text.Trim() != "") & (txtAuthors.Text.Trim() != ""));
        }
        #endregion

        #endregion
    }
}
